<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2019/5/25 0025
 * Time: 9:31
 */

namespace app\wap\controller;


use think\Db;

class Mission extends Middle
{
  
  
    //todo 签到
    public function Sign(){
        $week = $this->weekChange(time());
        $map = ['uid'=>$this->uid,$week=>0];
        $id = Db::name('sign')->where($map)->find();      
        if(!$id)return $this->returnError('今天已经签到过了');
        $sset = [$week=>1];
        $umap = ['uid'=>$this->uid];
        Db::startTrans();
        $price = Db::name('common')->where(['type'=>96])->value('content');
        if($this->vip == 1)$price = $price*2;
        if($this->vip == 2)$price = $price*4;
        $lianxu = $this->SignList();
        $res = $lianxu->getData()['data']['list'];
      	
        $i = 1;
        foreach ($res as $k=>$v){
            if($v['sign'] == 0)break;
            $i++;
        }
        $gift = 0;
        if($i == 7){
        	$gift = 0.05;
          	if($this->vip == 2) $gift *= 4*10;
       	 	if($this->vip == 1) $gift *= 3*10;
          	if($this->vip == 0) $gift *= 2*10;
        }        
        try{
            Db::name('sign')->where($map)->update($sset);
            Db::name('user')->where($umap)->setInc('integral',$price+$gift);
            $this->IntegralLog($this->uid,'签到',1,$price);
            if($i == 7)$this->IntegralLog($this->uid,'连续签到7天',1,$gift);
            Db::commit();
            return $this->returnSuccess(null,'签到成功');
        }catch (\Exception $e){
            Db::rollback();
            return $this->returnError($e->getMessage());
        }
    }

    //todo 获取签到记录
    public function SignList(){
        $map = ['uid'=>$this->uid];
        $list = Db::name('sign')
            ->where($map)
            ->find();
        $gift = Db::name('common')
            ->where(['type'=>'96'])
            ->value('content');
      	if($this->vip == 1)$gift *= 2;
      	if($this->vip == 2)$gift *= 4;
        $res = [
            'list'=>[['day'=>1,'sign'=>$list['one']],
                ['day'=>2,'sign'=>$list['two']],
                ['day'=>3,'sign'=>$list['three']],
                ['day'=>4,'sign'=>$list['four']],
                ['day'=>5,'sign'=>$list['five']],
                ['day'=>6,'sign'=>$list['six']],
                ['day'=>7,'sign'=>$list['seven']]],
            'gift' =>round($gift,2),
        ];
      	$a=0;
      	$date =  $this->weekChange(time());
      //	var_dump($date);die;
        $time= str_replace(['one','two','three','four','five','six','seven'],[1,2,3,4,5,6,7],$date);
      	if($list[$date] == 0)$time -=1;
 
        for ($i=0;$i<$time;$i++){
            if($res['list'][$i]['sign'] == 0){
                $a = 0;
            }else{
                $a++;
            }
        }
        $res['c'] = $a;
        return $this->returnSuccess($res);
    }

    //todo 获取分享内容
    public function ShareContent(){
       $ref = Db::name('common')
                ->where(['type'=>3])
                ->field('content,desc')
                ->find();
      	$ref['content'] .=$this->invitation; 
       return $this->returnSuccess($ref);

    }

    //todo 获取分享奖励
    public function ShareGift(){
        $map = ['uid'=>$this->uid];
        $count = Db::name('share_log')->where($map)->count();      
        if($count >= 1)return $this->returnError('分享次数超出限制,无法获得奖励');
        $gift = Db::name('common')->where(['type'=>4])->value('content');
        Db::startTrans();
        try{
            Db::name('user')->where(['uid'=>$this->uid])->setInc('integral',$gift);
            Db::name('share_log')->insert($map);
            $this->IntegralLog($this->uid,'分享奖励',1,$gift);
            Db::commit();
            return $this->returnSuccess(null,'获得分享奖励');
        }catch (\Exception $e){
            Db::rollback();
            return $this->returnError($e->getMessage());
        }
    }
}